//*****************************************************************************
// 1. Års eksamen @ Datamatiker - EASJ
// Holmegaard Heldagsskole Administrations system 
// 
// Denne klasse er udarbejdet af: Marc og Kasper
//*****************************************************************************
package view.elev;

import control.ControlUtil;
import javax.swing.DefaultListModel;
import model.*;

public class EvalueringsPanel extends javax.swing.JPanel {

    private ControlUtil controlUtil;
    private DefaultListModel fagModel = new DefaultListModel();

    public EvalueringsPanel(ControlUtil controlUtil) {
        this.controlUtil = controlUtil;
        initComponents();
    }

    public void hentElever() {
        int value = vaelgElevCombo.getSelectedIndex();
        vaelgElevCombo.removeAllItems();
        vaelgElevCombo.setEnabled(true);
        vaelgElevCombo.addItem("vælg Elev");
        Elev[] el = controlUtil.getElevHandler().hentElever();
        for (int i = 0; i < el.length; i++) {
            vaelgElevCombo.addItem(el[i]);
        }
        vaelgElevCombo.setSelectedIndex(value);
    }

    private void opdaterHandleplanOgEvaluering() {
        if (!elevFag.isSelectionEmpty()) {
            HoldFag faget = (HoldFag) elevFag.getSelectedValue();
            Elev elev = (Elev) vaelgElevCombo.getSelectedItem();
            Evaluering eval = controlUtil.getElevHandler().hentEvaluering(elev, faget.getFag());
            Handleplan handleplan = controlUtil.getElevHandler().hentHandleplan(elev, faget.getFag());

            if (erDetEnNyEvaluering(eval)) {
                evalueringArea.setText("");
            } else {
                evalueringArea.setText(eval.getEvalueringTekst());
            }
            if (erDetEnNyHandleplan(handleplan)) {
                handleplanarea.setText("");
            } else {
                handleplanarea.setText(handleplan.getHandleTekst());
            }
        } else {
            handleplanarea.setText("");
            evalueringArea.setText("");
        }
    }

    private void opdaterElev(Elev elev) {
        fagModel.clear();
        HoldFag[] elevensFag = controlUtil.getElevHandler().hentElevHoldFag(elev);
        for (int i = 0; i < elevensFag.length; i++) {
            fagModel.addElement(elevensFag[i]);
        }
    }

    private boolean erDetEnNyEvaluering(Evaluering eval) {
        boolean rtnbool = false;

        if (eval.getEvalueringTekst() == null) {
            rtnbool = true;
        }

        return rtnbool;
    }

    private boolean erDetEnNyHandleplan(Handleplan handleplan) {
        boolean rtnbool = false;

        if (handleplan.getHandleTekst() == null) {
            rtnbool = true;
        }
        return rtnbool;
    }

    private void ryd() {
        fagModel.clear();
        opdaterHandleplanOgEvaluering();
        handleplanKnap.setEnabled(false);
        evalueringKnap.setEnabled(false);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        vaelgElevLabel = new javax.swing.JLabel();
        vaelgElevCombo = new javax.swing.JComboBox();
        elevFagLabel = new javax.swing.JLabel();
        elevfagScroll = new javax.swing.JScrollPane();
        elevFag = new javax.swing.JList(fagModel);
        evalueringLabel = new javax.swing.JLabel();
        evalueringScroll = new javax.swing.JScrollPane();
        evalueringArea = new javax.swing.JTextArea();
        evalueringKnap = new javax.swing.JButton();
        handleplanLabel = new javax.swing.JLabel();
        handleplanScroll = new javax.swing.JScrollPane();
        handleplanarea = new javax.swing.JTextArea();
        handleplanKnap = new javax.swing.JButton();

        setMaximumSize(new java.awt.Dimension(1016, 696));
        setMinimumSize(new java.awt.Dimension(1016, 696));
        setPreferredSize(new java.awt.Dimension(1016, 696));
        setLayout(null);

        vaelgElevLabel.setText("Vælg Elev:");
        add(vaelgElevLabel);
        vaelgElevLabel.setBounds(60, 20, 80, 14);

        vaelgElevCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Ingen valgt" }));
        vaelgElevCombo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                vaelgElevComboActionPerformed(evt);
            }
        });
        add(vaelgElevCombo);
        vaelgElevCombo.setBounds(60, 40, 190, 20);

        elevFagLabel.setText("Elevens Fag:");
        add(elevFagLabel);
        elevFagLabel.setBounds(60, 90, 90, 14);

        elevFag.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                elevFagValueChanged(evt);
            }
        });
        elevfagScroll.setViewportView(elevFag);

        add(elevfagScroll);
        elevfagScroll.setBounds(60, 110, 190, 380);

        evalueringLabel.setText("Evaluering af eleven i faget:");
        add(evalueringLabel);
        evalueringLabel.setBounds(430, 280, 160, 10);

        evalueringArea.setColumns(20);
        evalueringArea.setRows(5);
        evalueringScroll.setViewportView(evalueringArea);

        add(evalueringScroll);
        evalueringScroll.setBounds(430, 300, 340, 160);

        evalueringKnap.setText("Gem evaluering");
        evalueringKnap.setEnabled(false);
        evalueringKnap.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                evalueringKnapActionPerformed(evt);
            }
        });
        add(evalueringKnap);
        evalueringKnap.setBounds(630, 470, 150, 40);

        handleplanLabel.setText("Handleplan af eleven i faget:");
        add(handleplanLabel);
        handleplanLabel.setBounds(430, 40, 160, 14);

        handleplanarea.setColumns(20);
        handleplanarea.setRows(5);
        handleplanScroll.setViewportView(handleplanarea);

        add(handleplanScroll);
        handleplanScroll.setBounds(430, 70, 340, 160);

        handleplanKnap.setText("Gem handleplan");
        handleplanKnap.setEnabled(false);
        handleplanKnap.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                handleplanKnapActionPerformed(evt);
            }
        });
        add(handleplanKnap);
        handleplanKnap.setBounds(620, 240, 150, 40);
    }// </editor-fold>//GEN-END:initComponents

    private void vaelgElevComboActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_vaelgElevComboActionPerformed

        if (vaelgElevCombo.getSelectedIndex() != 0 && vaelgElevCombo.getItemCount() != 0) {
            opdaterElev((Elev) vaelgElevCombo.getSelectedItem());
        } else {
            ryd();
        }

    }//GEN-LAST:event_vaelgElevComboActionPerformed

    private void elevFagValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_elevFagValueChanged

        if (elevFag.getSelectedIndex() == -1) {
            handleplanKnap.setEnabled(false);
            evalueringKnap.setEnabled(false);
        } else {
            handleplanKnap.setEnabled(true);
            evalueringKnap.setEnabled(true);
            opdaterHandleplanOgEvaluering();
        }
    }//GEN-LAST:event_elevFagValueChanged

    private void evalueringKnapActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_evalueringKnapActionPerformed

        HoldFag faget = (HoldFag) elevFag.getSelectedValue();
        Elev elev = (Elev) vaelgElevCombo.getSelectedItem();
        Evaluering eval = controlUtil.getElevHandler().hentEvaluering(elev, faget.getFag());
        if (erDetEnNyEvaluering(eval)) {
            //ny her
            Evaluering evaluering = new Evaluering(new EvalueringId(elev.getCpr(), faget.getFag().getFag()), elev, faget.getFag(), evalueringArea.getText());
            controlUtil.getElevHandler().nyEvaluering(evaluering);
        } else {
            //opdater
            eval.setEvalueringTekst(evalueringArea.getText());
            controlUtil.getElevHandler().opdaterEvaluering(eval);
        }
    }//GEN-LAST:event_evalueringKnapActionPerformed

    private void handleplanKnapActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_handleplanKnapActionPerformed
        HoldFag faget = (HoldFag) elevFag.getSelectedValue();
        Elev elev = (Elev) vaelgElevCombo.getSelectedItem();
        Handleplan handleplan = controlUtil.getElevHandler().hentHandleplan(elev, faget.getFag());
        if (erDetEnNyHandleplan(handleplan)) {
            //ny her
            Handleplan nyHp = new Handleplan(new HandleplanId(elev.getCpr(), faget.getFag().getFag()), elev, faget.getFag(), handleplanarea.getText());
            controlUtil.getElevHandler().nyHandleplan(nyHp);
        } else {
            //opdater
            handleplan.setHandleTekst(handleplanarea.getText());
            controlUtil.getElevHandler().opdaterHandleplan(handleplan);
        }

    }//GEN-LAST:event_handleplanKnapActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JList elevFag;
    private javax.swing.JLabel elevFagLabel;
    private javax.swing.JScrollPane elevfagScroll;
    private javax.swing.JTextArea evalueringArea;
    private javax.swing.JButton evalueringKnap;
    private javax.swing.JLabel evalueringLabel;
    private javax.swing.JScrollPane evalueringScroll;
    private javax.swing.JButton handleplanKnap;
    private javax.swing.JLabel handleplanLabel;
    private javax.swing.JScrollPane handleplanScroll;
    private javax.swing.JTextArea handleplanarea;
    private javax.swing.JComboBox vaelgElevCombo;
    private javax.swing.JLabel vaelgElevLabel;
    // End of variables declaration//GEN-END:variables
}
